package com.tyk.bookstore.back.order.service.impl;

import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.dal.mapper.dto.SinDto;
import com.tyk.bookstore.back.order.model.entity.OrderDo;
import com.tyk.bookstore.back.order.repository.OrderRepository;
import com.tyk.bookstore.back.order.service.OrderApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description OrderApiServiceImpl
 */
@Service
@RequiredArgsConstructor
public class OrderApiServiceImpl implements OrderApiService {

    private final OrderRepository orderRepository;

    @Override
    public List<BinVo> getOrderNoListByIdSet(Set<Long> idSet) {
        if (idSet.isEmpty()) return new ArrayList<>();
        return orderRepository.<BigInteger, String, Long>selectBinList1In(
                OrderDo::getId, OrderDo::getOrderNo,
                OrderDo::getId, idSet,
                OrderDo.class
        ).stream().map(it -> new BinVo(it.getV1().longValue(), it.getV2())).toList();
    }

    @Override
    public Long getIdByOrderNo(String orderNo) {
        SinDto<BigInteger> dto = orderRepository.selectSin1Eq(
                OrderDo::getId,
                OrderDo::getOrderNo, orderNo,
                OrderDo.class
        );
        return dto == null ? null : dto.getV1().longValue();
    }
}
